package com.qf.servlet;

import com.qf.entity.Card2;
import com.qf.entity.MyProducts;
import com.qf.entity.Products;
import com.qf.entity.PurdProducts;
import com.qf.service.Card2Service;
import com.qf.service.MyProductsService;
import com.qf.service.ProductsService;
import com.qf.service.PurdProductsService;
import com.qf.service.impl.Card2ServiceImpl;
import com.qf.service.impl.MyProductsServiceImpl;
import com.qf.service.impl.ProductsServiceImpl;
import com.qf.service.impl.PurdProductsServiceImpl;
import com.qf.utils.JdbcUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/proServlet")
public class ProductsServlet extends BaseServlet{

    private ProductsService ps = new ProductsServiceImpl();

    private MyProductsService ms = new MyProductsServiceImpl();

    private Card2Service cs = new Card2ServiceImpl();

    /*
        查询理财产品所有信息
     */
    private void findAllPro(HttpServletRequest req, HttpServletResponse resp){
        List<Products> Pros = ps.findAllPro();
        if (Pros != null){
            try {
                writeHTML(Pros,resp);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void findC2(HttpServletRequest req, HttpServletResponse resp){
        String uid = req.getParameter("uid");
        List<Card2> card2s = cs.findByUid(uid);
        if (card2s.size() > 0){
            try {
                writeHTML(card2s,resp);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void findAmountByC2ID(HttpServletRequest req, HttpServletResponse resp){
        String cid = req.getParameter("cid");
        Card2 card2 = cs.findCard2ById(cid);
        if (card2 != null){
            try {
                writeHTML(card2,resp);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*
        购买理财产品
    */
    private void buyNum(HttpServletRequest req, HttpServletResponse resp){
        String cid = req.getParameter("cid");
        String numStr = req.getParameter("num");
        int num = Integer.parseInt(numStr);
        String uidStr = req.getParameter("uid");
        int uid = Integer.parseInt(uidStr);
        String idStr = req.getParameter("id");
        int id = Integer.parseInt(idStr);
        Products p = ps.findByid(id);
        int pid = p.getId();
        String name = p.getName();
        double price = p.getPrice();
        String yieid = p.getYieid();
        String type = p.getType();
        double amount = num * price;
        MyProducts products = new MyProducts(id,name,uid,price,yieid,type,num,amount);
        Connection conn = JdbcUtils.getConnection();
        try {
            conn.setAutoCommit(false);
            MyProducts myProducts = ms.findByID(id);
            boolean flag = false;
            if (myProducts != null){
                if (myProducts.getId() == id){
                    double MyAmount = amount + myProducts.getAmount();
                    double MyNum = num + myProducts.getNum();
                    flag = ms.editMproByID(MyNum,MyAmount,id);
                }else {
                    flag = ms.addProByUid(products);

                }
            }else {
                flag = ms.addProByUid(products);
            }

            boolean flag2 = cs.jianqian(cid, amount);
            if (flag && flag2){
                conn.commit();
                resp.getWriter().print("Y");
            }else {
                resp.getWriter().print("N");
            }
        }catch (Exception e){
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }finally {
            JdbcUtils.closeAll(conn,null,null);
        }
    }
}
